onload = () => {
    loadCSS("../css/fontello.css", "bootstrap5-icon");
    loadCSS("../css/bootstrap5.min.css", "bootstrap5");
    loadCSS("../css/common.css", "common");
    loadScript("../js/bootstrap5.bundle.min.js")
        .then(() => loadScript("../js/crud.js")).then(() => {
        let billInfoOperator = new Crud({
            module: "billInfo",
            hasCheckbox: true,
            iterators: [],
            extendAttribute: [{name: "billInfoId", type: "arCharge"},
                {name: "updatedBy", type: "userById"}],
            opsButton: ["new","delete","export",{
                "caption": "Upload BillInfo CSV",
                "class": "btn btn-primary",
                hint: "Add GPS file",
                "action": "modal",
                "param": "#uploadQuoteFile",
                "commit": (form, e) => {
                    const fileInput = form.querySelector('#formFile');
                    const formData = new FormData();
                    formData.append('file', fileInput.files[0]);
                    const url = "/ar/bill/import";
                    return ajax(url, {method: "POST", body: formData, headers: {}}, data => {
                        showToast(`Upload successful: ${JSON.stringify(data.data)}`, "primary");
                        loadData();
                        let modalElement = document.getElementById("uploadQuoteFile");
                        const modal = bootstrap.Modal.getInstance(modalElement)
                            || new bootstrap.Modal(modalElement);
                        modal.hide();
                    }, error => showToast(`ERROR: description: ${error}`));
                }
            }],
            search: [
                {"caption": "Bill #", "type": "text", "name": "billNo", class: "col-3"}
            ],
            listField: [{"caption": "ID", "field": "billInfoId"},
                {caption: "BillNo", "field": "billNo"},
                 {caption: "Bill Time", "field": "billTime",format:str => subString(str,0,10)},
                {caption: "Update Time", "field": "updateTime",format:str => subString(str,0,10)},
                {caption: "UpdatedBy", "field": "updatedBy_userById.userName"}],
            editFields: [
                {caption:"Bill #", "name": "billNo", "placeHolder": "code", "required": "required"},
                {caption:"Bill Time","name": "billTime", type:"date", "required": "required"}
            ]
        });
        billInfoOperator.init();
        function loadData() {
            billInfoOperator.loadData();
        }
    }).catch((err) => console.error(err));
}
